#!/bin/bash -e
#
#  Copyright (C) 2015 ScyllaDB

if [ "`id -u`" -ne 0 ]; then
    echo "Requires root permission."
    exit 1
fi

print_usage() {
    echo "scylla_install_pkg --local-pkg /home/scylla/rpms --repo [URL]"
    echo "  --local-pkg	install locally built .rpm/.deb on specified directory"
    echo "  --repo  repository for both install and update, specify .repo/.list file URL"
    echo "  --repo-for-install  repository for install, specify .repo/.list file URL"
    echo "  --repo-for-update  repository for update, specify .repo/.list file URL"
    exit 1
}

LOCAL_PKG=
UNSTABLE=0
REPO_FOR_INSTALL=
REPO_FOR_UPDATE=
while [ $# -gt 0 ]; do
    case "$1" in
        "--local-pkg")
            LOCAL_PKG=$2
            shift 2
            ;;
        "--repo")
            REPO_FOR_INSTALL=$2
            REPO_FOR_UPDATE=$2
            shift 2
            ;;
        "--repo-for-install")
            REPO_FOR_INSTALL=$2
            shift 2
            ;;
        "--repo-for-update")
            REPO_FOR_UPDATE=$2
            shift 2
            ;;
        *)
            print_usage
            shift 1
            ;;
    esac
done

. /etc/os-release

if [ -f /etc/debian_version ]; then
    echo "#!/bin/sh" >> /usr/sbin/policy-rc.d
    echo "exit 101" >> /usr/sbin/policy-rc.d
    chmod +x /usr/sbin/policy-rc.d
    cp /etc/hosts /etc/hosts.orig
    echo 127.0.0.1 `hostname` >> /etc/hosts
    if [ "$REPO_FOR_INSTALL" != "" ]; then
        curl -o /etc/apt/sources.list.d/scylla_install.list $REPO_FOR_INSTALL
    fi
    apt-get update
    if [ "$LOCAL_PKG" = "" ]; then
        apt-get install -y --force-yes scylla
    else
        if [ ! -f /usr/bin/gdebi ]; then
            apt-get install -y --force-yes gdebi-core
        fi
        echo Y | gdebi $LOCAL_PKG/scylla-kernel-conf*.deb
        echo Y | gdebi $LOCAL_PKG/scylla-conf*.deb
        echo Y | gdebi $LOCAL_PKG/scylla-server_*.deb
        echo Y | gdebi $LOCAL_PKG/scylla-server-dbg*.deb
        echo Y | gdebi $LOCAL_PKG/scylla-jmx*.deb
        echo Y | gdebi $LOCAL_PKG/scylla-tools*.deb
        echo Y | gdebi $LOCAL_PKG/scylla_*.deb
    fi
    mv /etc/hosts.orig /etc/hosts
    rm /usr/sbin/policy-rc.d
    rm /etc/apt/sources.list.d/scylla_install.list
    if [ "$REPO_FOR_UPDATE" != "" ]; then
        curl -o /etc/apt/sources.list.d/scylla.list $REPO_FOR_UPDATE
    fi
    apt-get update
else
    if [ "$REPO_FOR_INSTALL" != "" ]; then
        curl -o /etc/yum.repos.d/scylla_install.repo $REPO_FOR_INSTALL
    fi

    if [ "$ID" = "centos" ]; then
            yum install -y epel-release
    elif [ "$ID" = "rhel" ]; then
        rpm -ivh http://download.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-7.noarch.rpm
    else
        echo "Unsupported distribution"
        exit 1
    fi

    if [ "$LOCAL_PKG" = "" ]; then
        yum install -y scylla
    else
        yum install -y $LOCAL_PKG/scylla*.*.rpm
    fi

    rm /etc/yum.repos.d/scylla_install.repo
    if [ "$REPO_FOR_UPDATE" != "" ]; then
        curl -o /etc/yum.repos.d/scylla.repo $REPO_FOR_UPDATE
    fi
fi
